// pages/home/home.js
const app = getApp()
var xrsdk = require('../../vendor/hope-sdk/core/request');
var config = require('../../config');
const util = require('../../utils/util.js');
var xrSdk = require('../../vendor/hope-sdk/index.js');
Page({
  /**
   * 页面的初始数据
   */
  data: {
    webURL: config.service.hostUrl,
    height: 64, //header高度
    top: 0, //标题图标距离顶部距离
    opcity: 0,
    banner:[],

    logined: false,
    authsetting: 0,
    userinfo: null,
    userDetail:null,
    addCarFlag:false,

  
    whiteHeight:'',
    circular:true,
    list:[0,1,2,3,4,5],
    display_items:5,
    bindCurrent:0,
    current:2,
    current_big:1,
    current_small:3,
    whiteBoolean:true,

    ids         :[7,3,4,2,5,8],
    loadding    :[false,false,false,false,false,false],
    pullUpOns   :[true,true,true,true,true,true],
    pageIndexs  :[1,1,1,1,1,1],
    productLists:[[],[],[],[],[],[]],
    top_loadding :[false,false,false,false,false,false],
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    //创建节点选择器
    var query = wx.createSelectorQuery();
    //选择id
    query.select('#content-box').boundingClientRect()
    let obj = wx.getMenuButtonBoundingClientRect();
    wx.getSystemInfo({
      success: (res) => {
        query.exec(function (res2) {
          //res就是 所有标签为mjltest的元素的信息 的数组
          //console.log(res);
          //取高度

          var bottomH = res2[0].height +30 - 45;
          that.setData({
            whiteHeight:res.windowHeight - bottomH
          })
        })
        this.setData({
          width: obj.left || res.windowWidth,
          height: obj.top ? (obj.top + obj.height + 8) : (res.statusBarHeight + 44),
          top: obj.top ? (obj.top + (obj.height - 32) / 2) : (res.statusBarHeight + 6)
        })
      }
    })
    var that = this;
    app.pageOnLoadInit(this).then(function (res) {
      that.initBanner();
    }, function (error) {
      // 登录失败
      wx.showModal({
        title: "Error",
        content: error.errMsg ? error.errMsg : 'Fail to login.Please feedback to manager.',
      });
      return false;
    });
    // for(var i = 0; i < 6 ;i++){
    //   // 数据
    //   that.initList(i);
    // }
  },

  initBanner: function () {
    var that = this;
    xrsdk.request(app,{
      url: config.service.advertUrl,
      login: false,
      data: {
        position: 51321
      },
      success: function (res) {
        that.setData({
          banner: res.data.data
        });
      }
    });
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    var that = this
    app.pageOnShowInit(that).then(function () {
      //这里写验证授权成功后 需要执行的逻辑
       that.initUserInfo();
       if (that.data.addCarFlag){
         wx.navigateTo({
           url: '../car/add/add?source=0'
         })
         that.setData({
           addCarFlag :false
         })
       }
    }, function () {
      //用户没有授权
      console.warn('Auth scope.userInfo is unable.');
      return false;
    });
    app.checkMessageCount();

  
      this.setData({
        loadding    :[false,false,false,false,false,false],
        pullUpOns   :[true,true,true,true,true,true],
        pageIndexs  :[1,1,1,1,1,1],
        productLists:[[],[],[],[],[],[]],
        top_loadding :[false,false,false,false,false,false],
      })
      for(var i = 0; i < 6 ;i++){
        // 数据
        that.initList(i);
      }
    
    
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },
  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  },
  // 滑到顶部刷新
  onBindscrolltoupper:function(){
    var current = this.data.current
    this.initData();
    this.initList(current);    
  },
  // 滑到底部
  onBindscrollDown:function(){
    var current = this.data.current
    if (!this.data.pullUpOns[current]) return;
    this.setData({
      ["loadding[" + current + "]"]: true
    }, () => {
      this.initList(current);
    })
  },
  //数据
  initList: function (index) {
    //var current = this.data.current;
    var that = this;
    xrsdk.request(app,{
      url: config.service.articleList,
      login: false,
      data: {
        page     : that.data.pageIndexs[index],
        page_size: 10,
        cid      : that.data.ids[index]
      },
      success: function (res) {
        console.log(res.data.data.data)
        if (res.data.data.has_more == false) {
          that.setData({
            ["pullUpOns[" + index + "]"]:false
          })
        }
        that.setData({
          ["productLists[" + index + "]"]: that.data.productLists[index].concat(res.data.data.data),
          ["pageIndexs[" + index + "]"]  : that.data.pageIndexs[index] + 1,
          ["loadding[" + index + "]"]    : false,
          ["top_loadding[" + index + "]"]: false
        })
        //wx.stopPullDownRefresh()
      }
    });
  },
  // 滑动swiper时
  onBindchange:function(e){
    var length = this.data.list.length
    var index = e.detail.current
    var current = index==length-2||index==length-1?index+2-length:index+2
    var current_big = current==0?length-1:current-1
    var current_small = current==length-1?0:current+1
    this.setData({
      current:current,
      current_big:current_big,
      current_small:current_small,
    })
  },
  //点击swiper时
  onBindCurrent:function(e){
    var length = this.data.list.length
    var index = e.currentTarget.dataset.index
    var bindCurrent = index==1||index==0?index+length-2:index-2
    var current = index
    var current_big = current==0?length-1:current-1
    var current_small = current==length-1?0:current+1
    this.setData({
      bindCurrent:bindCurrent,
      current:current,
      current_big:current_big,
      current_small:current_small
    })
  },
  initData: function () {
    var current         = this.data.current;
    this.setData({
      ["productLists[" + current + "]"]: [],
      ["pageIndexs[" + current + "]"]:1,
      ["pullUpOns["+ current + "]"] : true,
      ["loadding[" + current + "]"]:true,
      ["top_loadding[" + current + "]"]:false
    })
  },
  // 点击上弹
  bindButton:function(){
    this.setData({
      whiteBoolean:!this.data.whiteBoolean,
    })
  },
  href(e) {
    let page = Number(e.currentTarget.dataset.type)
    let url = "";
    switch (page) {
      case 0:  // 我的车辆
      url = "../car/list/list"
      break;
      case 1:  // 我的轮胎
      url = "../tyre/list/list"
      break;
      case 2:  // 更换次数
      url = "../order/myOrder/myOrder"
      break;
      case 3:  // 服务门店
      url = "../shop/list/list"
      break;
      case 4:  // 安装轮胎
      url = '../activity/apply/apply'
      break;
      case 5:  // 延保换新
      url = '../order/apply/apply'
      break;
      case 6:  // 道路救援
      url = '../rescue/apply/apply'
      break;
      case 7:  // 用户编辑
      url = '../my/userinfo/userinfo'
      break;
    }
    if (url && this.checkLogin()) {
      wx.navigateTo({
        url: url
      })
    }
  },
  checkLogin: function () {
    if (!this.data.logined) {  // 未登录
      wx.showModal({
        title: "Error",
        content: 'Fail to login.Please feedback to manager.',
      });
      return false;
    }
    if (this.data.authsetting == 0) {
      wx.navigateTo({
        url: "../auth/auth"
      })
      return false;
    }
    return true;
  },
  initUserInfo: function () {
    var that = this;
    xrsdk.request(app,{
      url: config.service.getUserDetail,
      login: true,
      success: function (res) {
        that.setData({
          userDetail: res.data.data,
        });
      },
      authfail: function (e) {
        wx.navigateTo({
          url: "../auth/auth"
        })
      }
    });
  },
  detail:function(e){
    var id = e.currentTarget.dataset.id;
    wx.navigateTo({
      url: '../article/detail?id='+id,
    })
  },
  brandHandle:function(e){
    var id = e.currentTarget.dataset.url;
    if(id){
      wx.navigateTo({
        url: '../article/detail?id='+id,
      })
    }
  }
})